<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAdd">添加</el-button>
    <el-button type="primary" @click="deleteBatch">批量删除</el-button>
    <el-button type="primary">导入</el-button>
    <el-button type="primary">导出</el-button>
    <el-table
      ref="filterTable"
      :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />

      <el-table-column
        prop="author"
        label="作者"
        width="120"
      />
      <el-table-column
        prop="name"
        label="试卷名"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="title"
        label="试卷标题"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="lastTime"
        label="考试时间(分钟)"
        :show-overflow-tooltip="true"
        width="135"
      />
      <el-table-column
        prop="score"
        label="试卷分数"
        :show-overflow-tooltip="true"
        width="135"
      />
      <el-table-column label="操作" width="210">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="handlePreview(scope.$index, scope.row)"
          >预览</el-button>
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <exam-modal ref="modalForm" />
  </div>
</template>

<script>
import { getExam, deleteExam, deleteBatchExam, preview } from '@/api/exam'
import ExamModal from './examModal.vue'
export default {
  components: { ExamModal },
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      currentPage: 1, // 初始页
      pagesize: 10 //    每页的数据
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getExam().then(res => {
        this.tableData = res.data.data.sort(function() {
          return 0.5 - Math.random()
        })
      })
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange(size) {
      this.pagesize = size
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
    },
    handleEdit(index, row) {
      this.$refs.modalForm.edit(row)
      this.$refs.modalForm.title = '编辑'
    },
    handleAdd() {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '添加'
    },
    handleDelete(index, row) {
      deleteExam({ id: row.id }).then(res => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.tableData.splice(index, 1)
      })
    },
    handlePreview(index, row) {
      preview(row).then(res => {
        if (typeof window.navigator.msSaveBlob !== 'undefined') { // IE10对象
          window.navigator.msSaveBlob(new Blob([res], {
            type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
          }), 'test.docx')
        } else { // 普通对象
          const url = window.URL.createObjectURL(new Blob([res], {
            type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
          }))
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', 'test.docx')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
        this.$message({
          message: '下载成功',
          type: 'success'
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(item => {
        return item.id
      })
      ids = ids.join(',')
      deleteBatchExam({ ids }).then(res => {
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.multipleSelection.forEach((item, index) => {
          // 遍历源数据
          this.tableData.forEach((itemI, indexI) => {
            // 如果选中数据跟元数据某一条标识相等，删除对应的源数据
            if (item.id === itemI.id) {
              this.tableData.splice(indexI, 1)
            }
          })
        })
        this.$refs.filterTable.clearSelection()
      })
    }
  }
}
</script>

